package com.huilian.iotbox.client.controller;

import com.huilian.iotbox.client.service.WeiXinService;
import com.huilian.iotbox.data.cmd.HuiLianDuiBiJiPositiveDataFrameCMD;
import com.huilian.iotbox.data.cmd.PositiveDataFrameCMD;
import com.huilian.iotbox.aliyun.service.PubService;
import com.huilian.iotbox.client.service.WeChatService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.WeiXinDto;
import com.huilian.iotbox.data.enums.ApiParamEnum;
import com.huilian.iotbox.data.properties.ConfigProperties;
import com.huilian.iotbox.data.utils.KaiSaEncrypt;
import com.huilian.iotbox.data.utils.Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;

/**
 * @Author: yjzhang
 * @Date: 2020/5/21 22:50
 */
@Controller
@RequestMapping("/api")
@Slf4j
public class WeiXinController extends BaseController {

    @Value("${huilian.iot.productKey}")
    private String productKey;
    @Autowired
    private PubService pubService;
    @Autowired
    private WeChatService weChatService;
    @Autowired
    private WeiXinService weiXinService;
    @Autowired
    public ConfigProperties configProperties;


    /**
     * 微信小程序登录
     *
     * @param request
     * @param weiXinDto
     * @return
     * @throws UnsupportedEncodingException
     */
    @ResponseBody
    @PostMapping("/applet/login")
    public ServerResponse appletLogin(HttpServletRequest request, @RequestBody WeiXinDto weiXinDto) throws IOException {
        return weChatService.appletLogin(weiXinDto, request);
    }

    /**
     * 扫设备码先到这里，然后重定向请求中间件获取用户信息，再重定向回下个接口"/biz/login"
     *
     * @param deviceNo
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping("/deviceNo/{deviceNo}")
    public String host(@PathVariable String deviceNo, Model model, HttpServletRequest request) throws UnsupportedEncodingException {
        StringBuilder params = new StringBuilder(deviceNo);
        params.append("^");
        params.append(ApiParamEnum.CLIENT_CURRENCY.getCode());
        return weChatService.biz(params.toString(), model, request);
    }

    @GetMapping("/getOpenIdByOpen")
    public String getOpenIdByOpen(HttpServletRequest request, Model model) throws UnsupportedEncodingException {
        return weChatService.getOpenIdByOpen(request, model);
    }

    @GetMapping("/c")
    public String host2(String d, String h, String f, Model model, HttpServletRequest request) throws UnsupportedEncodingException {
        StringBuilder params = new StringBuilder(d);
        params.append("^");
        params.append(ApiParamEnum.CLIENT_CURRENCY.getCode());

        if (!StringUtils.isEmpty(h) && !StringUtils.isEmpty(f)){
            params.append("^");
            params.append(h);
            params.append("^");
            params.append(f);
        }

        return weChatService.biz(params.toString(), model, request);
    }

    /**
     *
     * @param merchantStoreId
     * @param model
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping("/memberCenter")
    public String memberCenter(String merchantStoreId, Model model, HttpServletRequest request) throws UnsupportedEncodingException {
        StringBuilder params = new StringBuilder();
//        String attStr =  URLEncoder.encode(String.valueOf(att), "UTF-8");
//        params.append("^");
//        params.append(ApiParamEnum.VENDMACH_ANDROID.getCode());
//        params.append("^");
//        params.append(attStr);
        return weChatService.biz(params.toString(), model, request);
    }


    /**
     * 获取到用户信息回调
     *
     * @param request
     * @return
     */
    @GetMapping("/biz/login")
    public String login(HttpServletRequest request, Model model) throws UnsupportedEncodingException {
        return weChatService.login(request, model);
    }

    @GetMapping("/weixin/getMemberInfo")
    public String getMemberInfo(String d, String officialAccountOpenId, String h, String f, Model model, HttpServletRequest request) throws UnsupportedEncodingException {
        StringBuilder params = new StringBuilder(d);
        params.append("^");
        params.append(ApiParamEnum.CLIENT_CURRENCY.getCode());

        if (!StringUtils.isEmpty(h) && !StringUtils.isEmpty(f)){
            params.append("^");
            params.append(h);
            params.append("^");
            params.append(f);
        }

        return weChatService.getMemberInfo(d, params.toString(), officialAccountOpenId, model, request);
    }

    /**
     * 获取用户信息（根据openID参数获取用户信息）
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping("/weixin/outPutUserInfoByOpenId")
    public String outPutUserInfoByOpenId(HttpServletRequest request, Model model) throws UnsupportedEncodingException {
        return weChatService.outPutUserInfoByOpenId(request, model);
    }


    @ResponseBody
    @GetMapping("/weixin/getWxJsSdkConfig")
    public ServerResponse getWxJsSdkConfig(WeiXinDto weiXinDto, HttpServletRequest request) throws Exception {
        return weiXinService.getWxJsSdkConfig(weiXinDto);
    }
}
